<template>
  <div id="home">
    <nav-bar class="nav-home">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control
        :titles="['流行', '新款', '精选']"
        @tabClick="titleClick"
        ref="tabcontrol1"
        v-show="isControlShow"
        class="control"
      ></tab-control>
    <scroll
      class="scroll"
      ref="scroll"
      :prob="3"
      @scroll="backtoTOP"
      :pull-up-load="true"
      @pullingUp="loadMore"
    >
      <home-swiper :banners="banners" class="homebanner" @SwiperImgLoad="getOffsetTop" v-if="banners"></home-swiper>
      <recommend-view :recommends="recommends"></recommend-view>
      <feature-view></feature-view>
      <tab-control
        :titles="['流行', '新款', '精选']"
        @tabClick="titleClick"
        ref="tabcontrol2"
        :class="{tabcontrol:isControlShow}"
      ></tab-control>
      <goods-list :goods="goods[currentType].list"></goods-list>
    </scroll>
    <back-top @click.native="backCLick" v-show="isShow"></back-top>
  </div>
</template>

<script>
import NavBar from "components/common/navbar/NavBar.vue";
import TabControl from "components/content/tabControl/TabControl.vue";
import GoodsList from "components/content/goods/GoodsList.vue";
import Scroll from "components/common/scroll/Scroll.vue";
import BackTop from "components/content/backTop/BackTop.vue";

import HomeSwiper from "./childComps/HomeSwiper";
import RecommendView from "./childComps/RecommendView";
import FeatureView from "./childComps/FeatureView";

import { getHomeMultidata, getHomeGoods } from "network/home";

import {itemListemer} from 'common/mixin'
export default {
  name: "home",
  data() {
    return {
      banners: null,
      recommends: [],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      currentType: "pop",
      isShow: false,
      offsetTop:0,
      isControlShow:false,

    };
  },
  components: {
    NavBar,
    TabControl,
    GoodsList,
    Scroll,
    BackTop,

    HomeSwiper,
    RecommendView,
    FeatureView,
  },
  activated() {
      this.$refs.scroll.scrollTo(0,this.leaveY)
      this.$refs.scroll.refresh()
    },
   /*  deactivated() {
      this.leaveY = this.$refs.scroll.y;
    }, */
  deactivated(){
     this.leaveY = this.$refs.scroll.scroll.y;
    // 离开时取消全局事件监听
    this.$bus.$off('imageItemLoad',this.ItemListener)
  },
  created() {
    this.getHomeMultidata();
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
  },
    mixins:[itemListemer],

  methods: {
    //事件监听相关的方法
    titleClick(index) {
      if (index == 0) {
        this.currentType = "pop";
      } else if (index == 1) {
        this.currentType = "new";
      } else {
        this.currentType = "sell";
      }
      this.$refs.tabcontrol2.currentIndex=index;
      this.$refs.tabcontrol1.currentIndex=index;
    },
    getOffsetTop(){
      this.offsetTop=this.$refs.tabcontrol2.$el.offsetTop
    },
    // 网络请求相关的方法
    async getHomeMultidata() {
      let { data: res } = await getHomeMultidata();
      this.banners = res.banner.list;
      this.recommends = res.recommend.list;
    },
    async getHomeGoods(type) {
      let page = this.goods[type].page + 1;
      let { data: res } = await getHomeGoods(type, page);
      this.goods[type].list.push(...res.list);
      this.goods[type].page += 1;
      this.$refs.scroll.finishPullUp();
    },
    backCLick() {
      this.$refs.scroll.scrollTo(0, 0,300);
    },
    backtoTOP(position) {
      this.isShow = -position.y > 1500 ? true : false;
      this.isControlShow=(-position.y) >this.offsetTop ? true :false
   
    },
    loadMore() {
      this.getHomeGoods(this.currentType);
    },
  },
};
</script>

<style>
#home {
  position: relative;
  height: 100vh;
}
.nav-home {
  color: #fff;
  background-color: var(--color-tint);
  font-size: 16px;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 9;
}
.control{
  position: relative;
  margin-top: 44px;
  z-index: 9;
  background-color: #fff;
}
.tabcontrol{
  position: fixed;
  top: 44px;
  left: 0;
  right: 0;
}
.scroll {
  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
  overflow: hidden;
}
</style>